package com.chinaisafe.user.service.impl;

import com.chinaisafe.user.entity.SysAccount;
import com.chinaisafe.user.mapper.SysAccountMapper;
import com.chinaisafe.user.service.SysAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaisafe.user.vo.request.SysAccountReqVO;
import com.chinaisafe.user.vo.response.SysAccountRespVO;
import org.springframework.stereotype.Service;
import com.chinaisafe.base.common.exception.BizException;
import com.chinaisafe.base.mybatisplus.constant.MybatisplusConstants;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaisafe.base.mybatisplus.util.MpQueryWrapperPageUtil;

import java.util.Arrays;
import java.util.Optional;
/**
 * SysAccountServiceImpl
 * Description：账号服务实现类
 *
 * @author xuefu.shu
 * @date 2024-07-12 09:55:54
 */
@Service
public class SysAccountServiceImpl extends ServiceImpl<SysAccountMapper, SysAccount> implements SysAccountService {

    @Override
    public IPage<SysAccountRespVO> getPageList(SysAccountReqVO sysAccountReqVO) {
        // 分页设置
        Page<SysAccount> page = MpQueryWrapperPageUtil.buildWrapperPage(new Page<>(), sysAccountReqVO);
        // 查询条件构造
        QueryWrapper<SysAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysAccount::getDeleted, MybatisplusConstants.LOGIC_NOT_DELETE_VALUE);
        return baseMapper.getPageList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysAccountRespVO add(SysAccountReqVO sysAccountReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysAccountReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysAccountRespVO update(SysAccountReqVO sysAccountReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysAccountReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysAccountRespVO addOrUpdate(SysAccountReqVO sysAccountReqVO) {
        Long sysAccountId = sysAccountReqVO.getId();
        if (sysAccountId != null){
            // 编辑
            SysAccount sysAccount = baseMapper.selectById(sysAccountId);
            Optional.ofNullable(sysAccount).orElseThrow(() -> BizException.build("未找到要修改的账号信息"));
        }
        SysAccount sysAccount2Save = sysAccountReqVO.convertToEntity();
        boolean success = this.saveOrUpdate(sysAccount2Save);
        if (success) {
            return SysAccountRespVO.convertFromEntity(sysAccount2Save);
        } else {
            BizException.throwException("操作失败，请刷新后重试");
        }
        return null;
    }

    @Override
    public SysAccountRespVO getDetailById(Long sysAccountId) {
        SysAccount sysAccount = baseMapper.selectById(sysAccountId);
        Optional.ofNullable(sysAccount).orElseThrow(() -> BizException.build("未找到对应账号信息"));
        return SysAccountRespVO.convertFromEntity(sysAccount);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(Long[] sysAccountIds) {
        if (sysAccountIds.length > 0) {
            this.removeByIds(Arrays.asList(sysAccountIds));
        }
    }
}
